/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "connector_dev.h"
#include "io.h"
#include "drv_gpio.h"
#include "k_vo_comm.h"
#include "k_connector_comm.h"

#define DELAY_MS_BACKLIGHT_DEFAULT     200
#define DELAY_MS_BACKLIGHT_FIRST       1

static k_s32 g_blacklight_delay_ms = DELAY_MS_BACKLIGHT_FIRST;


static void st7701_480x800_init(k_u8 test_mode_en)
{
#if defined (CONFIG_BOARD_K230_CANMV_LCKFB)
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x05, 0, 1, 0x01,
        0x05,10, 1, 0x11,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
        0x15, 0, 2, 0xD1, 0x11,
        0x15, 0, 2, 0x55, 0xB0,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x10,
        0x39, 0, 3, 0xC0, 0x63, 0x00,
        0x39, 0, 3, 0xC1, 0x04, 0x02,
        0x39, 0, 3, 0xC2, 0x37, 0x08,
        0x15, 0, 2, 0xC7, 0x00,
        0x15, 0, 2, 0xCC, 0x38,
        0x39, 0,17, 0xB0, 0x00, 0x11, 0x19, 0x0C, 0x10, 0x06, 0x07, 0x0A, 0x09, 0x22, 0x04, 0x10, 0x0E, 0x28, 0x30, 0x1C,
        0x39, 0,17, 0xB1, 0x00, 0x12, 0x19, 0x0D, 0x10, 0x04, 0x06, 0x07, 0x08, 0x23, 0x04, 0x12, 0x11, 0x28, 0x30, 0x1C,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
        0x15, 0, 2, 0xB0, 0x4D,
        0x15, 0, 2, 0xB1, 0x60,
        0x15, 0, 2, 0xB2, 0x07,
        0x15, 0, 2, 0xB3, 0x80,
        0x15, 0, 2, 0xB5, 0x47,
        0x15, 0, 2, 0xB7, 0x8A,
        0x15, 0, 2, 0xB8, 0x21,
        0x15, 0, 2, 0xC1, 0x78,
        0x15, 0, 2, 0xC2, 0x78,
        0x15, 0, 2, 0xD0, 0x88,
        0x39,10, 4, 0xE0, 0x00, 0x00, 0x02,
        0x39, 0,12, 0xE1, 0x01, 0xA0, 0x03, 0xA0, 0x02, 0xA0, 0x04, 0xA0, 0x00, 0x44, 0x44,
        0x39, 0,12, 0xE2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x39, 0, 5, 0xE3, 0x00, 0x00, 0x33, 0x33,
        0x39, 0, 3, 0xE4, 0x44, 0x44,
        0x39, 0,17, 0xE5, 0x01, 0x26, 0xA0, 0xA0, 0x03, 0x28, 0xA0, 0xA0, 0x05, 0x2A, 0xA0, 0xA0, 0x07, 0x2C, 0xA0, 0xA0,
        0x39, 0, 5, 0xE6, 0x00, 0x00, 0x33, 0x33,
        0x39, 0, 3, 0xE7, 0x44, 0x44,
        0x39, 0,17, 0xE8, 0x02, 0x26, 0xA0, 0xA0, 0x04, 0x28, 0xA0, 0xA0, 0x06, 0x2A, 0xA0, 0xA0, 0x08, 0x2C, 0xA0, 0xA0,
        0x39, 0, 8, 0xEB, 0x00, 0x01, 0xE4, 0xE4, 0x44, 0x00, 0x40,
        0x39, 0,17, 0xED, 0xFF, 0xF7, 0x65, 0x4F, 0x0B, 0xA1, 0xCF, 0xFF, 0xFF, 0xFC, 0x1A, 0xB0, 0xF4, 0x56, 0x7F, 0xFF,
        0x15, 0, 2, 0xEE, 0x42, 
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x15, 0, 2, 0x36, 0x00,
        0x15, 0, 2, 0x3A, 0x55,
        0x05,10, 1, 0x11,
        0x05, 0, 1, 0x29,
    };
#else
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x15, 0, 2, 0xEF, 0x08,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x10,
        0x39, 0, 3, 0xC0, 0x63, 0x00,
        0x39, 0, 3, 0xC1, 0x10, 0x02,
        0x39, 0, 3, 0xC2, 0x31, 0x02,
        0x15, 0, 2, 0xCC, 0x10,
        0x39, 0,17, 0xB0, 0xC0, 0x0C, 0x92, 0x0C, 0x10, 0x05, 0x02, 0x0D, 0x07, 0x21, 0x04, 0x53, 0x11, 0x6A, 0x32, 0x1F,
        0x39, 0,17, 0xB1, 0xC0, 0x87, 0xCF, 0x0C, 0x10, 0x06, 0x00, 0x03, 0x08, 0x1D, 0x06, 0x54, 0x12, 0xE6, 0xEC, 0x0F,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
        0x15, 0, 2, 0xB0, 0x5D,
        0x15, 0, 2, 0xB1, 0x62,
        0x15, 0, 2, 0xB2, 0x82,
        0x15, 0, 2, 0xB3, 0x80,
        0x15, 0, 2, 0xB5, 0x42,
        0x15, 0, 2, 0xB7, 0x85,
        0x15, 0, 2, 0xB8, 0x20,
        0x15, 0, 2, 0xC0, 0x09,
        0x15, 0, 2, 0xC1, 0x78,
        0x15, 0, 2, 0xC2, 0x78,
        0x15, 0, 2, 0xD0, 0x88,
        0x15, 0, 2, 0xEE, 0x42,
        0x39,10, 4, 0xE0, 0x00, 0x00, 0x02,
        0x39, 0,12, 0xE1, 0x04, 0xA0, 0x06, 0xA0, 0x05, 0xA0, 0x07, 0xA0, 0x00, 0x44, 0x44,
        0x39, 0,13, 0xE2, 0x00, 0x00, 0x33, 0x33, 0x01, 0xA0, 0x00, 0x00, 0x01, 0xA0, 0x00, 0x00,
        0x39, 0, 5, 0xE3, 0x00, 0x00, 0x33, 0x33,
        0x39, 0, 3, 0xE4, 0x44, 0x44,
        0x39, 0,17, 0xE5, 0x0C, 0x30, 0xA0, 0xA0, 0x0E, 0x32, 0xA0, 0xA0, 0x08, 0x2C, 0xA0, 0xA0, 0x0A, 0x2E, 0xA0, 0xA0,
        0x39, 0, 5, 0xE6, 0x00, 0x00, 0x33, 0x33,
        0x39, 0, 3, 0xE7, 0x44, 0x44,
        0x39, 0,17, 0xE8, 0x0D, 0x31, 0xA0, 0xA0, 0x0F, 0x33, 0xA0, 0xA0, 0x09, 0x2D, 0xA0, 0xA0, 0x0B, 0x2F, 0xA0, 0xA0,
        0x39, 0, 8, 0xEB, 0x00, 0x01, 0xE4, 0xE4, 0x44, 0x88, 0x00,
        0x39, 0,17, 0xED, 0xFF, 0xF5, 0x47, 0x6F, 0x0B, 0xA1, 0xA2, 0xBF, 0xFB, 0x2A, 0x1A, 0xB0, 0xF6, 0x74, 0x5F, 0xFF,
        0x39, 0, 7, 0xEF, 0x08, 0x08, 0x08, 0x40, 0x3F, 0x64,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x39, 0, 3, 0xE8, 0x00, 0x0E,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x05,10, 1, 0x11,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x39, 0, 3, 0xE8, 0x00, 0x0C,
        0x39, 0, 3, 0xE8, 0x00, 0x00,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x15, 0, 2, 0x3A, 0x50,
        0x05, 0, 1, 0x29,
    };
#endif

    connector_send_cmd(init_sequence, sizeof(init_sequence), K_FALSE);
}

static void st7701_480x854_init(k_u8 test_mode_en)
{
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x15,10, 2, 0x11, 0x00,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x10,
        0x39, 0, 3, 0xC0, 0xE9, 0x03,                      /* (105 + 1) * 8 + 3 x 2 = 854 */
        0x39, 0, 3, 0xC1, 0x12, 0x02,                      /* vbp = 0x12  =18   vfp = 0x2 */
        0x39, 0, 3, 0xC2, 0x31, 0x08,                      /* pclk = 512 + (0x8 x 16) = 640 */
        0x15, 0, 2, 0xCC, 0x10,
        0x39, 0,17, 0xB0, 0x00, 0x0A, 0x13, 0x0E, 0x12, 0x07, 0x05, 0x08, 0x08, 0x1F, 0x07, 0x15, 0x13, 0xE3, 0x2A, 0x11,
        0x39, 0,17, 0xB1, 0x00, 0x0A, 0x12, 0x0E, 0x12, 0x07, 0x04, 0x07, 0x07, 0x1E, 0x04, 0x13, 0x10, 0x23, 0x29, 0x11,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
        0x15, 0, 2, 0xB0, 0x4D,
        0x15, 0, 2, 0xB1, 0x1C,
        0x15, 0, 2, 0xB2, 0x07,
        0x15, 0, 2, 0xB3, 0x80,
        0x15, 0, 2, 0xB5, 0x47,
        0x15, 0, 2, 0xB7, 0x85,
        0x15, 0, 2, 0xB8, 0x21,
        0x15, 0, 2, 0xB9, 0x10,
        0x15, 0, 2, 0xC1, 0x78,
        0x15, 0, 2, 0xC2, 0x78,
        0x15, 0, 2, 0xD0, 0x88,
        0x39, 10, 4, 0xE0, 0x00, 0x00, 0x02,
        0x39, 0,12, 0xE1, 0x0B, 0x00, 0x0D, 0x00, 0x0C, 0x00, 0x0E, 0x00, 0x00, 0x44, 0x44,
        0x39, 0,14, 0xE2, 0x33, 0x33, 0x44, 0x44, 0x64, 0x00, 0x66, 0x00, 0x65, 0x00, 0x67, 0x00, 0x00,
        0x39, 0, 5, 0xE3, 0x00, 0x00, 0x33, 0x33,
        0x39, 0, 3, 0xE4, 0x44, 0x44,
        0x39, 0,17, 0xE5, 0x0C, 0x78, 0xA0, 0xA0, 0x0E, 0x78, 0xA0, 0xA0, 0x10, 0x78, 0xA0, 0xA0, 0x12, 0x78, 0xA0, 0xA0,
        0x39, 0, 5, 0xE6, 0x00, 0x00, 0x33, 0x33,
        0x39, 0, 3, 0xE7, 0x44, 0x44, 
        0x39, 0,17, 0xE8, 0x0D, 0x78, 0xA0, 0xA0, 0x0F, 0x78, 0xA0, 0xA0, 0x11, 0x78, 0xA0, 0xA0, 0x13, 0x78, 0xA0, 0xA0,
        0x39, 0, 8, 0xEB, 0x02, 0x00, 0x39, 0x39, 0xEE, 0x44, 0x00,
        0x39, 0, 3, 0xEC, 0x00, 0x00, 
        0x39, 0,17, 0xED, 0xFF, 0xF1, 0x04, 0x56, 0x72, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF3, 0x27, 0x65, 0x40, 0x1F, 0xFF,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x15, 5, 2, 0x29, 0x00,
    };

    connector_send_cmd(init_sequence, sizeof(init_sequence), K_FALSE);
}

static void st7701_480x640_init(k_u8 test_mode_en)
{
#if defined (CONFIG_BOARD_K230D_CANMV_ATK_DNK230D)
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x15, 0, 2, 0xEF, 0x08,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x10,
        0x39, 0, 3, 0xC0, 0x4F, 0x00,
        0x39, 0, 3, 0xC1, 0x10, 0x0C,
        0x39, 0, 3, 0xC2, 0x01, 0x14,
        0x15, 0, 2, 0xCC, 0x10,
        0x39, 0,17, 0xB0, 0x00, 0x0B, 0x13, 0x0D, 0x10, 0x07, 0x02, 0x08, 0x07, 0x1F, 0x04, 0x11, 0x0F, 0x28, 0x2F, 0x1F,
        0x39, 0,17, 0xB1, 0x00, 0x0C, 0x13, 0x0C, 0x10, 0x05, 0x02, 0x08, 0x08, 0x1E, 0x05, 0x13, 0x11, 0x27, 0x30, 0x1F,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
        0x15, 0, 2, 0xB0, 0x4D,
        0x15, 0, 2, 0xB1, 0x4D,
        0x15, 0, 2, 0xB2, 0x87,
        0x15, 0, 2, 0xB3, 0x80,
        0x15, 0, 2, 0xB5, 0x45,
        0x15, 0, 2, 0xB7, 0x85,
        0x15, 0, 2, 0xB8, 0x20,
        0x15, 0, 2, 0xC0, 0x09,
        0x15, 0, 2, 0xC1, 0x78,
        0x15, 0, 2, 0xC2, 0x78,
        0x15,10, 2, 0xD0, 0x88,
        0x39, 0, 4, 0xE0, 0x00, 0x00, 0x02,
        0x39, 0,12, 0xE1, 0x04, 0xB0, 0x06, 0xB0, 0x05, 0xB0, 0x07, 0xB0, 0x00, 0x44, 0x44,
        0x39, 0,13, 0xE2, 0x20, 0x20, 0x44, 0x44, 0x96, 0xA0, 0x00, 0x00, 0x96, 0xA0, 0x00, 0x00,
        0x39, 0, 5, 0xE3, 0x00, 0x00, 0x22, 0x22,
        0x39, 0, 3, 0xE4, 0x44, 0x44,
        0x39, 0,17, 0xE5, 0x0C, 0x90, 0xB0, 0xA0, 0x0E, 0x92, 0xB0, 0xA0, 0x08, 0x8C, 0xB0, 0xA0, 0x0A, 0x8E, 0xB0, 0xA0,
        0x39, 0, 5, 0xE6, 0x00, 0x00, 0x22, 0x22,
        0x39, 0, 3, 0xE7, 0x44, 0x44,
        0x39, 0,17, 0xE8, 0x0D, 0x91, 0xB0, 0xA0, 0x0F, 0x93, 0xB0, 0xA0, 0x09, 0x8D, 0xB0, 0xA0, 0x0B, 0x8F, 0xB0, 0xA0,
        0x39, 0, 3, 0xE9, 0x36, 0x00,
        0x39, 0, 8, 0xEB, 0x00, 0x00, 0xE4, 0xE4, 0x44, 0x88, 0x40,
        0x39, 0,17, 0xED, 0xC1, 0xA2, 0xBF, 0x0F, 0x67, 0x45, 0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x76, 0xF0, 0xFB, 0x2A, 0x1C,
        0x39, 0, 7, 0xEF, 0x10, 0x0D, 0x04, 0x08, 0x3F, 0x1F,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x39, 0, 3, 0xE8, 0x00, 0x0E,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x05,20, 1, 0x11,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x39,10, 3, 0xE8, 0x00, 0x0C,
        0x39, 0, 3, 0xE8, 0x00, 0x00,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x05,20, 1, 0x29,
    };
#else
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x15, 0, 2, 0xEF, 0x08,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x10,
        0x39, 0, 3, 0xC0, 0x4F, 0x00,
        0x39, 0, 3, 0xC1, 0x10, 0x0C,
        0x39, 0, 3, 0xC2, 0x31, 0x14,
        0x15, 0, 2, 0xCC, 0x10,
        0x39, 0,17, 0xB0, 0x00, 0x0B, 0x13, 0x0D, 0x10, 0x07, 0x02, 0x08, 0x07, 0x1F, 0x04, 0x11, 0x0F, 0x28, 0x2F, 0x1F,
        0x39, 0,17, 0xB1, 0x00, 0x0C, 0x13, 0x0C, 0x10, 0x05, 0x02, 0x08, 0x08, 0x1E, 0x05, 0x13, 0x11, 0x27, 0x30, 0x1F,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x11,
        0x15, 0, 2, 0xB0, 0x5d,
        0x15, 0, 2, 0xB1, 0x5d,
        0x15, 0, 2, 0xB2, 0x87,
        0x15, 0, 2, 0xB3, 0x80,
        0x15, 0, 2, 0xB5, 0x45,
        0x15, 0, 2, 0xB7, 0x85,
        0x15, 0, 2, 0xB8, 0x20,
        0x15, 0, 2, 0xC0, 0x09,
        0x15, 0, 2, 0xC1, 0x78,
        0x15, 0, 2, 0xC2, 0x78,
        0x15,100,2, 0xD0, 0x88,
        0x39, 0, 4, 0xE0, 0x00, 0x00, 0x02,
        0x39, 0,12, 0xE1, 0x04, 0xB0, 0x06, 0xB0, 0x05, 0xB0, 0x07, 0xB0, 0x00, 0x44, 0x44,
        0x39, 0,13, 0xE2, 0x20, 0x20, 0x44, 0x44, 0x96, 0xA0, 0x00, 0x00, 0x96, 0xA0, 0x00, 0x00,
        0x39, 0, 5, 0xE3, 0x00, 0x00, 0x22, 0x22,
        0x39, 0, 3, 0xE4, 0x44, 0x44,
        0x39, 0,17, 0xE5, 0x0C, 0x90, 0xB0, 0xA0, 0x0E, 0x92, 0xB0, 0xA0, 0x08, 0x8C, 0xB0, 0xA0, 0x0A, 0x8E, 0xB0, 0xA0,
        0x39, 0, 5, 0xE6, 0x00, 0x00, 0x22, 0x22,
        0x39, 0, 3, 0xE7, 0x44, 0x44,
        0x39, 0,17, 0xE8, 0x0D, 0x91, 0xB0, 0xA0, 0x0F, 0x93, 0xB0, 0xA0, 0x09, 0x8D, 0xB0, 0xA0, 0x0B, 0x8F, 0xB0, 0xA0,
        0x39, 0, 3, 0xE9, 0x36, 0x00,
        0x39, 0, 8, 0xEB, 0x00, 0x00, 0xE4, 0xE4, 0x44, 0x88, 0x40,
        0x39, 0,17, 0xED, 0xC1, 0xA2, 0xBF, 0x0F, 0x67, 0x45, 0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x76, 0xF0, 0xFB, 0x2A, 0x1C,
        0x39, 0, 7, 0xEF, 0x10, 0x0D, 0x04, 0x08, 0x3F, 0x1F,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x39, 0, 3, 0xE8, 0x00, 0x0E,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x15,120,2, 0x11, 0x00,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x13,
        0x39,10, 3, 0xE8, 0x00, 0x0C,
        0x39, 0, 3, 0xE8, 0x00, 0x00,
        0x39, 0, 6, 0xFF, 0x77, 0x01, 0x00, 0x00, 0x00,
        0x15, 0, 2, 0x3A, 0x70, // 0x50:565, 0x60:666, 0x70:888
        0x15,20, 2, 0x29, 0x00
    };
#endif

    connector_send_cmd(init_sequence, sizeof(init_sequence), K_FALSE);
}

static void st7701_368x552_init(k_u8 test_mode_en)
{
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x39, 0,  6,  0xFF,0x77,0x01,0x00,0x00,0x13,
        0x15, 0,  2,  0xEF,0x08,
        0x39, 0,  6,  0xFF,0x77,0x01,0x00,0x00,0x10,
        0x39, 0,  3,  0xC0,0x44,0x00,
        0x39, 0,  3,  0xC1,0x0B,0x02,
        0x39, 0,  3,  0xC2,0x07,0x1F,
        0x15, 0,  2,  0xCC,0x10,
        0x39, 0,  17, 0xB0,0x0F,0x1E,0x25,0x0D,0x11,0x06,0x12,0x08,0x08,0x2A,0x05,0x12,0x10,0x2B,0x32,0x1F,
        0x39, 0,  17, 0xB1,0x0F,0x1E,0x25,0x0D,0x11,0x05,0x12,0x08,0x08,0x2B,0x05,0x12,0x10,0x2B,0x32,0x1F,
        0x39, 0,  6,  0xFF,0x77,0x01,0x00,0x00,0x11,
        0x15, 0,  2,  0xB0,0x35,
        0x15, 0,  2,  0xB1,0x45,
        0x15, 0,  2,  0xB2,0x87,
        0x15, 0,  2,  0xB3,0x80,
        0x15, 0,  2,  0xB5,0x49,
        0x15, 0,  2,  0xB7,0x85,
        0x15, 0,  2,  0xB8,0x11,
        0x15, 0,  2,  0xBB,0x03,
        0x15, 0,  2,  0xC0,0x07,
        0x15, 0,  2,  0xC1,0x78,
        0x15, 0,  2,  0xC2,0x78,
        0x15, 100,2,  0xD0,0x88,
        0x39, 0,  4,  0xE0,0x00,0x00,0x02,
        0x39, 0,  12, 0xE1,0x03,0x30,0x07,0x30,0x02,0x30,0x06,0x30,0x00,0x44,0x44,
        0x39, 0,  12, 0xE2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x39, 0,  5,  0xE3,0x00,0x00,0x22,0x00,
        0x39, 0,  3,  0xE4,0x22,0x00,
        0x39, 0,  17, 0xE5,0x0A,0x34,0x30,0xE0,0x08,0x32,0x30,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x39, 0,  5,  0xE6,0x00,0x00,0x22,0x00,
        0x39, 0,  3,  0xE7,0x22,0x00,
        0x39, 0,  17, 0xE8,0x09,0x33,0x30,0xE0,0x07,0x31,0x30,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x39, 0,  8,  0xEB,0x00,0x01,0x10,0x10,0x11,0x00,0x00,
        0x39, 0,  17, 0xED,0xFF,0xFF,0xF0,0x45,0xBA,0x2F,0xFF,0xFF,0xFF,0xFF,0xF2,0xAB,0x54,0x0F,0xFF,0xFF,
        0x39, 0,  7,  0xEF,0x08,0x08,0x08,0x45,0x3F,0x54,
        0x39, 0,  6,  0xFF,0x77,0x01,0x00,0x00,0x13,
        0x39, 0,  3,  0xE8,0x00,0x0E,
        0x15, 120,2,  0x11,0x00,
        0x39, 10, 3,  0xE8,0x00,0x0C,
        0x39, 0,  3,  0xE8,0x00,0x00,
        0x39, 0,  6,  0xFF,0x77,0x01,0x00,0x00,0x00,
        0x15, 50, 2,  0x29,0x00,
    };

    connector_send_cmd(init_sequence, sizeof(init_sequence), K_FALSE);
}

static void st7701_power_reset(k_s32 on)
{
    k_u8 rst_gpio;
    if(0 > (rst_gpio = CONFIG_MPP_DSI_LCD_RESET_PIN)) {
        return;
    }
    // rt_kprintf("rst_gpio is %d \n",rst_gpio);

    kd_pin_mode(rst_gpio, GPIO_DM_OUTPUT);

    if (on)
        kd_pin_write(rst_gpio, GPIO_PV_HIGH); // GPIO_PV_LOW  GPIO_PV_HIGH
    else
        kd_pin_write(rst_gpio, GPIO_PV_LOW); // GPIO_PV_LOW  GPIO_PV_HIGH

}

static void st7701_set_backlight(k_s32 on)
{
    k_u8 backlight_gpio;
    if(0 > (backlight_gpio = CONFIG_MPP_DSI_LCD_BACKLIGHT_PIN)) {
        return;
    }

    // rt_kprintf("backlight_gpio is %d \n",backlight_gpio);

    kd_pin_mode(backlight_gpio, GPIO_DM_OUTPUT);
    if (on)
        kd_pin_write(backlight_gpio, GPIO_PV_HIGH);
    else
        kd_pin_write(backlight_gpio, GPIO_PV_LOW);

}


static k_s32 st7701_power_on(void* ctx, k_s32 on)
{
    k_s32 ret = 0;
    struct connector_driver_dev* dev = ctx;
    // rst vo;
    k230_display_rst();

    if (on) {

        // rst st7701
        st7701_power_reset(1);
        rt_thread_mdelay(g_blacklight_delay_ms);
        st7701_power_reset(0);
        rt_thread_mdelay(g_blacklight_delay_ms);
        st7701_power_reset(1);

        g_blacklight_delay_ms = DELAY_MS_BACKLIGHT_DEFAULT;
        //enable backlight
        st7701_set_backlight(1);
    } else {
        st7701_set_backlight(0);
    }

    return ret;
}


static k_s32 st7701_set_phy_freq(k_connectori_phy_attr *phy_attr)
{
    k_vo_mipi_phy_attr mipi_phy_attr;

    memset(&mipi_phy_attr, 0, sizeof(k_vo_mipi_phy_attr));

    mipi_phy_attr.m = phy_attr->m;
    mipi_phy_attr.n = phy_attr->n;
    mipi_phy_attr.hs_freq = phy_attr->hs_freq;
    mipi_phy_attr.voc = phy_attr->voc;
    mipi_phy_attr.phy_lan_num = K_DSI_4LAN;
    connector_set_phy_freq(&mipi_phy_attr);
    // rt_kprintf("config phy success phy_attr->m is %d  phy_attr->n is %d phy_attr->hs_freq is %d phy_attr->voc is %d \n", phy_attr->m,  phy_attr->n, phy_attr->hs_freq, phy_attr->voc);
    return 0;
}


static k_s32 st7701_dsi_resolution_init(k_connector_info *info)
{
    k_vo_dsi_attr attr;
    k_vo_display_resolution resolution;

    memset(&attr, 0, sizeof(k_vo_dsi_attr));
    attr.lan_num = info->lan_num;
    attr.cmd_mode = info->cmd_mode;
    attr.lp_div = 8;
    attr.work_mode = info->work_mode;
    memcpy(&resolution, &info->resolution, sizeof(k_vo_display_resolution));
    memcpy(&attr.resolution, &resolution, sizeof(k_vo_display_resolution));
    connector_set_dsi_attr(&attr);

    if(info->type == ST7701_V1_MIPI_2LAN_480X800_30FPS)
    {
        if(info->screen_test_mode)
            st7701_480x800_init(1);
        else
            st7701_480x800_init(0);
    }
    else if(info->type == ST7701_V1_MIPI_2LAN_480X854_30FPS)
    {
        if(info->screen_test_mode)
            st7701_480x854_init(1);
        else
            st7701_480x854_init(0);
    }
    else if(info->type == ST7701_V1_MIPI_2LAN_480X640_30FPS)
    {
        if(info->screen_test_mode)
            st7701_480x640_init(1);
        else
            st7701_480x640_init(0);
    }
    else if (info->type == ST7701_V1_MIPI_2LAN_368X544_60FPS)
    {
        if(info->screen_test_mode)
            st7701_368x552_init(1);
        else
            st7701_368x552_init(0);
    }

    connector_set_dsi_enable(1);

    if(info->dsi_test_mode == 1)
        connector_set_dsi_test_mode();

    return 0;
}


static k_s32 st7701_vo_resolution_init(k_vo_display_resolution *resolution, k_u32 bg_color, k_u32 intr_line)
{
    k_vo_display_resolution vo_resolution;
    k_vo_pub_attr attr;

    memset(&attr, 0, sizeof(k_vo_pub_attr));
    attr.bg_color = bg_color;
    attr.intf_sync = K_VO_OUT_1080P30;
    attr.intf_type = K_VO_INTF_MIPI;
    attr.sync_info = resolution;

    connector_set_vo_init();
    connector_set_vtth_intr(1, intr_line);
    connector_set_vo_param(&attr);
    connector_set_vo_enable();

    return 0;
}


k_s32 st7701_init(void *ctx, k_connector_info *info)
{
    k_s32 ret = 0;
    struct connector_driver_dev* dev = ctx;

    if(info->pixclk_div != 0)
        connector_set_pixclk(info->pixclk_div);

    ret |= connector_set_cmd_buff_num(info->buff_num);
    ret |= st7701_set_phy_freq(&info->phy_attr);
    ret |= st7701_dsi_resolution_init(info);
    ret |= st7701_vo_resolution_init(&info->resolution, info->bg_color, info->intr_line);

    return ret;
}

static k_s32 st7701_get_chip_id(void* ctx, k_u32* chip_id)
{
    k_s32 ret = 0;

    return ret;
}


static k_s32 st7701_conn_check(void* ctx, k_s32* conn)
{
    k_s32 ret = 0;

    *conn = 1;

    return ret;
}

struct connector_driver_dev st7701_connector_drv = {
    .connector_name = "st7701",
    .connector_func = {
        .connector_power = st7701_power_on,
        .connector_init = st7701_init,
        .connector_get_chip_id = st7701_get_chip_id,
        .connector_conn_check = st7701_conn_check,
    },
};
